package com.zhaoxi.project.sys.controller;

import java.util.ArrayList;
import java.util.List;

import jakarta.servlet.http.HttpServletResponse;

import com.zhaoxi.project.sys.service.impl.SysDictDataServiceImpl;
import com.zhaoxi.project.sys.service.impl.SysDictTypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhaoxi.common.utils.StringUtils;
import com.zhaoxi.common.utils.poi.ExcelUtil;
import com.zhaoxi.framework.aspectj.lang.annotation.Log;
import com.zhaoxi.framework.aspectj.lang.enums.BusinessType;
import com.zhaoxi.framework.web.BaseService;
import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.framework.web.page.TableDataInfo;
import com.zhaoxi.project.sys.domain.SysDictData;

/**
 * 数据字典信息
 *
 * @author zhaoxi
 */
@RestController
@RequestMapping("/sys/dict/data")
public class SysDictDataController extends BaseService {
    @Autowired
    private SysDictDataServiceImpl dictDataService;

    @Autowired
    private SysDictTypeServiceImpl dictTypeService;

    /**
     * 1、字典数据管理：根据条件分页查询字典数据列表（xieleilei 12.6 已测试）
     */
    @PreAuthorize("@ss.hasPermi('sys:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDictData dictData) {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    /**
     * 2、字典数据管理：新增字典数据类型（xieleilei 12.6 已测试）
     */
    @PreAuthorize("@ss.hasPermi('sys:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public PageData add(@Validated @RequestBody SysDictData dict) {
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 3、字典数据管理：修改字典数据（xieleilei 12.6 已测试）
     */
    @PreAuthorize("@ss.hasPermi('sys:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public PageData edit(@Validated @RequestBody SysDictData dict) {
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 4、删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('sys:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public PageData remove(@PathVariable Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermi('sys:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public PageData getInfo(@PathVariable Long dictCode) {
        return success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public PageData dictType(@PathVariable String dictType) {
        SysDictData dictData = new SysDictData();
        dictData.setDictType(dictType);
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictData);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<SysDictData>();
        }
        return success(data);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/typeList")
    public TableDataInfo typeList(SysDictData dictData) {
        startPage();
        List<SysDictData> list = dictTypeService.selectDictDataByType(dictData);
        return getDataTable(list);
    }

    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('sys:dict:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictData dictData) {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        util.exportExcel(response, list, "字典数据");
    }

}
